import {useCallback, useEffect, useRef, useState} from 'react';
import {
  LineChart,
  type ChartChangeEvent,
} from '@see_you/react-native-charts-wrapper';

export const useChartScale = (data: any[]) => {
  const [scaleX, setScaleX] = useState(1);
  const chartRef = useRef<LineChart>(null);
  useEffect(() => {
    setScaleX(pre => {
      if (pre !== 1) {
        chartRef.current?.fitScreen();
      }
      return 1;
    });
  }, [data]);
  const onChange = useCallback((e: ChartChangeEvent) => {
    if (e.nativeEvent.action === 'chartScaled') {
      setScaleX(e.nativeEvent?.scaleX ?? 1);
    }
  }, []);
  return {
    chartRef,
    scaleX,
    onChange,
  };
};
